package com.ivy.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ivy.entity.TContent;
import com.ivy.entity.Teacher;
import com.ivy.service.TContentService;
import com.ivy.service.impl.TContentServiceImpl;
import com.ivy.utils.DataReadUtils;

public class TContentServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public TContentServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	private TContentService tcService = new TContentServiceImpl();
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//response.setContentType("image/png");  
		PrintWriter out = response.getWriter();
		
		String opreation = request.getParameter("opreation");
		if("getCookie".equals(opreation)) {
			getCookie(request,response);
		} else if("getTContent".equals(opreation)) {
			getTContent(request,response);
		}

	}
	private void getTContent(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException{
		String valCode = request.getParameter("valCode");

		String tid = request.getParameter("tid");
		
		String cookie = tcService.getCookie();
		System.out.println(cookie+" this is getTContent ");
		List<TContent> tcontents = tcService.getAllTContentByTid( cookie, valCode, tid);
		
		System.out.println(cookie+"============servlet");
		
		JSONArray arr = new JSONArray();
		for(int i=0;i<tcontents.size();i++) {
			TContent tc = tcontents.get(i);
			if(tc.getInfo().length()<5){
				continue;
			} else{
				
				JSONObject obj = new JSONObject();
				
				try {
					obj.put("info", tc.getInfo());
					obj.put("tid", tc.getTid());
					obj.put("weekday", tc.getWeekday());
					obj.put("time", tc.getTime());
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
				arr.put(obj);
			}
			
		}
		
		response.getWriter().print(arr);
		
	}

	private void getCookie(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException{

		byte[] imageBuf = tcService.getValImg();
	
		String cookie = tcService.getCookie();
		System.out.println(cookie+"this is getCookie....");
		for(int i = 0 ; i < imageBuf.length ; i++){
			response.getWriter().print(imageBuf[i]);
		}	
		
		JSONObject obj = new JSONObject();
		try {
			obj.put("cookie", cookie);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
